package sliver.action.account;

import java.util.Map;

import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import sliver.entity.Shopper;
import sliver.service.ShopperAccountManager;

import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller("account.shopperLoginAction")
public class ShopperLoginAction extends ActionSupport implements CookiesAware, SessionAware{

	private static final long serialVersionUID = -2462061382366325956L;
	
	private String username;
	private String password;
	private String isRememberMe;
	
	private Map<String, String> cookies;
	private Map<String, Object> session;
	
	@Autowired
	private ShopperAccountManager shopperAccountManager;
	
	
	public ShopperLoginAction(){
		
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getIsRememberMe() {
		return isRememberMe;
	}

	public void setIsRememberMe(String isRememberMe) {
		this.isRememberMe = isRememberMe;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setCookiesMap(Map<String, String> cookies) {
		this.cookies = cookies;
	}
	
	@Override
	public String execute() throws Exception {
		
		if(username == null || "".equals(username)){
			return INPUT;
		}
		
		if(password == null || "".equals(password)){
			return INPUT;
		}
		
		Shopper shopper = shopperAccountManager.shopperLogin(username, password);
		
		if(shopper != null){
			if("yes".equals(isRememberMe))
				cookies.put("cookie_shopper_username_password", shopper.getUsername()+"|"+shopper.getPassword());
			
			session.put("login_shopper", shopper);
			return SUCCESS;
		}
		
		return ERROR;
	}
}